﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _9.StringWithMaxLength
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strings = {  
                                   // null, 
                                   "ala bala", 
                                   "ala bala3", 
                                   "ala bala very long", 
                                   "sho", 
                                   "short", 
                               };

            Console.WriteLine(GetStringWithMaxLen(strings));
        }

        static string GetStringWithMaxLen(string[] strings)
        {
            if (strings.Length == 0)
            {
                return null;
            }

            //var maxString = from s in strings
            //                orderby s.Length descending
            //                select s;

            //return maxString.FirstOrDefault();            

            var lengths = from s in strings
                          select (s != null ? s.Length : 0);

            var maxLength = lengths.Max();

            return strings.First(s => s.Length == maxLength);
        }

        static string GetStringWithMaxLen2(string[] strings)
        {
            if (strings.Length == 0)
            {
                return null;
            }


            var lengths = strings.Select(s => s.Length);
            var maxLength = lengths.Max();

            return strings.First(s => s.Length == maxLength);
        }
    }
}
